package com.self.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.self.commonutils.R;
import com.self.statistics.client.UcenterClient;
import com.self.statistics.entity.StatisticsDaily;
import com.self.statistics.mapper.StatisticsDailyMapper;
import com.self.statistics.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author xiaozeng
 * @since 2020-10-10
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    //统计某一天的注册人数,生成统计数据
    @Override
    public void registerCount(String day) {

        //添加记录前删除表中日期相同的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);

        //远程调用得到莫一天的注册人数
        R registerR = ucenterClient.countRegister(day);
        Integer countRegister = (Integer) registerR.getData().get("countRegister");

        //创建统计对象
        StatisticsDaily daily = new StatisticsDaily();
        daily.setRegisterNum(countRegister);//注册人数
        daily.setDateCalculated(day);//统计日期

        daily.setLoginNum(RandomUtils.nextInt(100,200));//TODO
        daily.setVideoViewNum(RandomUtils.nextInt(100,200));//TODO
        daily.setCourseNum(RandomUtils.nextInt(100,200));//TODO

        daily.setGmtCreate(new Date());
        daily.setGmtModified(new Date());

        baseMapper.insert(daily);


    }

    //图表显示，返回两部分：日期json数组，数量json数组
    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {

        //根据条件查询数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> list = baseMapper.selectList(wrapper);

        //返回两部分数据： 日期和数量，前端需要的是JSON数据
        //后端list会变成前端的JSON字符串。map会变成JSON对象
        List<String> date_calculatedList = new ArrayList<>();
        List<Integer> numDataList = new ArrayList<>();

        //遍历所有数据的List，分别封装进日期和数量的list
        for (int i = 0; i < list.size(); i++) {
            StatisticsDaily daily = list.get(i);
            //封装日期集合
            date_calculatedList.add(daily.getDateCalculated());
            //封装对应数量
            switch (type){
                case "login_num":
                    numDataList.add(daily.getLoginNum());
                    break;
                case "register_num":
                    numDataList.add(daily.getRegisterNum());
                    break;
                case "video_view_num":
                    numDataList.add(daily.getVideoViewNum());
                case "course_num":
                    numDataList.add(daily.getCourseNum());
                default:
                    break;
            }
        }

        HashMap<String, Object> map = new HashMap<>();
        map.put("date_calculatedList",date_calculatedList);
        map.put("numDataList",numDataList);

        return map;
    }
}
